package com.hsgene.user.service.impl;

import com.hsgene.user.condition.GeneticTestingUserAddressCondition;
import com.hsgene.user.condition.GroundServantInfoCondition;
import com.hsgene.user.domain.GeneticTestingUserAddress;
import com.hsgene.user.dto.address.AddressDetails;
import com.hsgene.user.dto.address.GeneticTestingUserAddressDetails;
import com.hsgene.user.dto.address.GeneticTestingUserAddressDto;
import com.hsgene.user.dto.user.groundservant.GroundServantInfoDto;
import com.hsgene.user.persistence.GeneticTestingUserAddressRepository;
import com.hsgene.user.service.GeneticTestingUserAddressService;
import com.hsgene.user.service.api.GeneratorService;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @projectName: hdas-geneshop-server
 * @package: com.hsgene.user.service.impl
 * @author: maodi
 * @createDate: 2018/11/8 9:08
 * @version: 1.0
 * Copyright: Copyright (c) 2018
 */
@Service
public class GeneticTestingUserAddressImpl implements GeneticTestingUserAddressService {

    @Resource
    private GeneticTestingUserAddressRepository geneticTestingUserAddressRepository;

    @Resource
    GeneratorService generatorService;

    /**
     * @param territoryName 大区名字
     * @return int
     * @description 查询大区名称在字典表的数量
     * @author maodi
     * @createDate 2018/11/8 17:08
     */
    @Override
    public int queryCountSysDictByTerritoryName(String territoryName) {
        return geneticTestingUserAddressRepository.queryCountSysDictByTerritoryName(territoryName);
    }

    /**
     * @param name 省，市，区县名称
     * @return int
     * @description 查询省，市，区县名称在地区表中的数量
     * @author maodi
     * @createDate 2018/11/8 17:09
     */
    @Override
    public int queryCountSysAreaByName(String name) {
        return geneticTestingUserAddressRepository.queryCountSysAreaByName(name);
    }

    /**
     * @param areaId 地区id
     * @return int
     * @description 获取地区id在地区表中的数量
     * @author maodi
     * @createDate 2018/11/8 17:19
     */
    @Override
    public int queryCountSysAreaByAreaId(String areaId) {
        return geneticTestingUserAddressRepository.queryCountSysAreaByAreaId(areaId);
    }

    /**
     * @param
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @description 获取地服下拉框数据源
     * @author maodi
     * @createDate 2018/11/8 16:02
     */
    @Override
    public List<Map<String, Object>> selectGroundServant() {
        return geneticTestingUserAddressRepository.selectGroundServant();
    }


    /**
     * @param userId 用户id
     * @return GeneticTestingUserAddressDetails
     * @description 根据用户id获取地服基本信息
     * @author maodi
     * @createDate 2018/11/8 18:16
     */
    @Override
    public GeneticTestingUserAddressDetails queryGroundServantBaseInfo(String userId) {
        return geneticTestingUserAddressRepository.queryGroundServantBaseInfo(userId);
    }

    /**
     * @param userId
     * @return java.util.List<com.hsgene.user.dto.address.AddressDetails>
     * @description 根据用户id地服收货地址详情
     * @author maodi
     * @createDate 2018/11/8 18:17
     */
    @Override
    public List<AddressDetails> queryAddressDetails(String userId) {
        return geneticTestingUserAddressRepository.queryAddressDetails(userId);
    }

    /**
     * @param condition 查询条件
     * @return int
     * @description 地服收货地址条件查询数量
     * @author maodi
     * @createDate 2018/11/8 18:18
     */
    @Override
    public int queryGroundServantCountByCondition(GeneticTestingUserAddressCondition condition) {
        return geneticTestingUserAddressRepository.queryGroundServantCountByCondition(condition);
    }

    /**
     * @param condition 查询条件
     * @return java.util.List<com.hsgene.user.dto.address.GeneticTestingUserAddressDto>
     * @description 地服收货地址条件查询
     * @author maodi
     * @createDate 2018/11/9 9:16
     */
    @Override
    public List<GeneticTestingUserAddressDto> queryGroundServantByCondition(GeneticTestingUserAddressCondition
                                                                                    condition) {
        return geneticTestingUserAddressRepository.queryGroundServantByCondition(condition);
    }

    /**
     * @param id 逻辑主键
     * @return java.util.List<com.hsgene.user.domain.GeneticTestingUserAddress>
     * @description 地服收货地址根据id查询
     * @author maodi
     * @createDate 2018/11/9 9:17
     */
    @Override
    public List<GeneticTestingUserAddress> queryGroundServantById(String id) {
        return geneticTestingUserAddressRepository.queryGroundServantById(id);
    }

    /**
     * @param userId 用户id
     * @return java.util.List<com.hsgene.user.domain.GeneticTestingUserAddress>
     * @description 地服收货地址根据用户id查询
     * @author maodi
     * @createDate 2018/11/9 9:18
     */
    @Override
    public List<GeneticTestingUserAddress> queryGroundServantByUserId(String userId) {
        return geneticTestingUserAddressRepository.queryGroundServantByUserId(userId);
    }

    /**
     * @param list   地服收货地址列表
     * @param userId 用户id
     * @return void
     * @description 地服收货地址新增
     * @author maodi
     * @createDate 2018/11/12 15:37
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void insertGroundServant(List<GeneticTestingUserAddress> list, String userId) {
        //如果没有默认，将第一个设置默认
        List<Integer> defaultList = new ArrayList<>();
        for (GeneticTestingUserAddress address : list) {
            defaultList.add(address.getIsDefaultAddress());
        }
        Boolean setDefault = false;
        if (!defaultList.contains(1)) {
            setDefault = true;
        }
        Date date = new Date();
        for (GeneticTestingUserAddress address : list) {
            String id = generatorService.getGenerator("", "").getData().getSerialNumber();
            address.setId(id);
            geneticTestingUserAddressRepository.insertGroundServant(address);
            int isDefault = address.getIsDefaultAddress();
            if (setDefault) {
                isDefault = 1;
                setDefault = false;
            }
            if (isDefault == 1) {
                geneticTestingUserAddressRepository.setDefaultAddress(id, date);
                geneticTestingUserAddressRepository.setOtherNotDefaultAddress(id, userId, date);
            } else {
                geneticTestingUserAddressRepository.setNotDefaultAddress(id, date);
            }
        }
    }

    /**
     * @param id 逻辑主键id
     * @return int
     * @description 设置此id的地服收货地址为默认
     * @author maodi
     * @createDate 2018/11/9 9:23
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int setDefaultAddress(String id) {
        return geneticTestingUserAddressRepository.setDefaultAddress(id, new Date());
    }

    /**
     * @param id 逻辑主键id
     * @return int
     * @description 设置此id的地服收货地址为非默认
     * @author maodi
     * @createDate 2018/11/12 16:12
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int setNotDefaultAddress(String id) {
        return geneticTestingUserAddressRepository.setNotDefaultAddress(id, new Date());
    }

    /**
     * @param id     逻辑主键id
     * @param userId 用户id
     * @return int
     * @description 设置除当前id外此用户id的其他地服收货地址为非默认
     * @author maodi
     * @createDate 2018/11/9 9:23
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int setOtherNotDefaultAddress(String id, String userId) {
        return geneticTestingUserAddressRepository.setOtherNotDefaultAddress(id, userId, new Date());
    }

    /**
     * @param id
     * @param userId
     * @return int
     * @description 设置此id的地服收货地址为默认，除此id外此用户id的其他地服收货地址为非默认
     * @author maodi
     * @createDate 2018/11/9 13:37
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int setCurrentDefaultAndSetOtherNotDefault(String id, String userId) {
        int count = 0;
        Date date = new Date();
        count += geneticTestingUserAddressRepository.setDefaultAddress(id, date);
        count += geneticTestingUserAddressRepository.setOtherNotDefaultAddress(id, userId, date);
        return count;
    }

    /**
     * @param list   地服收货地址链表
     * @param userId 用户id
     * @return void
     * @description 地服收货地址修改
     * @author maodi
     * @createDate 2018/11/12 15:42
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public void updateGroundServant(List<GeneticTestingUserAddress> list, String userId) {
        List<String> newIdList = new ArrayList<>();
        List<String> oldIdList = geneticTestingUserAddressRepository.queryIdsByUserId(userId);
        Date date = new Date();
        for (GeneticTestingUserAddress address : list) {
            String id = address.getId();
            //新增
            if (StringUtils.isBlank(id)) {
                id = generatorService.getGenerator("", "").getData().getSerialNumber();
                address.setId(id);
                if (oldIdList.contains(id)) {
                    geneticTestingUserAddressRepository.updateGroundServant(address);
                } else {
                    geneticTestingUserAddressRepository.insertGroundServant(address);
                }
            }
            //修改
            else {
                address.setCreateDateTime(null);
                geneticTestingUserAddressRepository.updateGroundServant(address);
            }
            newIdList.add(id);
            int isDefault = address.getIsDefaultAddress();
            if (isDefault == 1) {
                geneticTestingUserAddressRepository.setDefaultAddress(id, date);
                geneticTestingUserAddressRepository.setOtherNotDefaultAddress(id, userId, date);
            } else {
                geneticTestingUserAddressRepository.setNotDefaultAddress(id, date);
            }
        }
        //删除
        for (String oldId : oldIdList) {
            if (!newIdList.contains(oldId)) {
                GeneticTestingUserAddress address = new GeneticTestingUserAddress();
                address.setId(oldId);
                address.setUpdateDateTime(date);
                geneticTestingUserAddressRepository.deleteGroundServant(address);
            }
        }
    }

    /**
     * @param geneticTestingUserAddress 地服收货地址
     * @return int
     * @description 地服收货地址删除
     * @author maodi
     * @createDate 2018/11/9 9:25
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int deleteGroundServant(GeneticTestingUserAddress geneticTestingUserAddress) {
        return geneticTestingUserAddressRepository.deleteGroundServant(geneticTestingUserAddress);
    }

    /**
     * @param userIds 用户id数组
     * @return int
     * @description 地服收货地址根据用户id批量删除
     * @author maodi
     * @createDate 2018/11/9 9:27
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor =
            Exception.class)
    @Override
    public int deleteGroundServantMulti(String[] userIds) {
        return geneticTestingUserAddressRepository.deleteGroundServantMulti(userIds, new Date());
    }

    /**
     * @param userIds 用户id数组
     * @return int
     * @description 根据用户id数组查询数量
     * @author maodi
     * @createDate 2018/11/9 16:50
     */
    @Override
    public int queryCountByUserIds(String[] userIds) {
        return geneticTestingUserAddressRepository.queryCountByUserIds(userIds);
    }

    /**
     * @param id 地区表的主键id
     * @return java.lang.String
     * @description 根据id获取地区表中名称
     * @author maodi
     * @createDate 2018/12/24 9:27
     */
    @Override
    public String queryNameByAreaId(String id) {
        return geneticTestingUserAddressRepository.queryNameByAreaId(id);
    }

    /**
     * @param userId 用户id
     * @return java.util.List<java.lang.String>
     * @description 查询用户id所有的主键id
     * @author maodi
     * @createDate 2018/11/9 17:01
     */
    @Override
    public List<String> queryIdsByUserId(String userId) {
        return geneticTestingUserAddressRepository.queryIdsByUserId(userId);
    }

    /**
     * @param name 真实姓名
     * @return int
     * @description 根据地服真实姓名查找用户表中数量
     * @author maodi
     * @createDate 2018/11/12 10:17
     */
    @Override
    public int queryGeneticTestingUserCountByRealName(String name) {
        return geneticTestingUserAddressRepository.queryGeneticTestingUserCountByRealName(name);
    }

    /**
     * @param userId 用户id
     * @param name   用户真实姓名
     * @return int
     * @description 根据用户id和用户真实姓名查询用户表中数量
     * @author maodi
     * @createDate 2018/11/12 10:28
     */
    @Override
    public int queryGeneticTestingUserCountByUserIdAndRealName(String userId, String name) {
        return geneticTestingUserAddressRepository.queryGeneticTestingUserCountByUserIdAndRealName(userId, name);
    }

    /**
     * @param userId 用户id
     * @return int
     * @description 根据用户id查找地服收货地址默认数量
     * @author maodi
     * @createDate 2018/11/12 11:15
     */
    @Override
    public int queryIsDefaultCountByUserId(String userId) {
        return geneticTestingUserAddressRepository.queryIsDefaultCountByUserId(userId);
    }

    /**
     * @param condition 查询条件
     * @return java.util.List<com.hsgene.user.dto.user.groundservant.GroundServantInfoDto>
     * @description 根据查询条件获取地服列表信息
     * @author maodi
     * @createDate 2018/11/21 11:32
     */
    @Override
    public List<GroundServantInfoDto> queryGroundServantInfoByCondition(GroundServantInfoCondition condition) {
        return geneticTestingUserAddressRepository.queryGroundServantInfoByCondition(condition);
    }

    /**
     * @param condition 查询条件
     * @return int
     * @description 根据查询条件查询地服数量
     * @author maodi
     * @createDate 2018/11/21 11:45
     */
    @Override
    public int queryGroundServantInfoCountByCondition(GroundServantInfoCondition condition) {
        return geneticTestingUserAddressRepository.queryGroundServantInfoCountByCondition(condition);
    }

}
